define(function(require){
    $(function(){
    	/*---------- 策略管理——平台运营人员 ----------*/
    	var traffic_id = semi.utils.getUrlParam('traffic_id'),
    		resource_id = semi.utils.getUrlParam('resource');
    	
    	/* 表格 */
    	var bodyTemplate = ['<tr strategy_id="{id}">',
	    	                    '<td class="txtc">{name}</td>',
	    	                    '<td class="txtc strategy_state">{state}</td>',
	    	                    '<td class="txtc">{start} 至 {end}</td>',
	    	                    '<td class="txtc hours">{hours}</td>',
	    	                    '<td class="txtc areas">{areas}</td>',
	    	                    '<td class="txtc areas">{words}</td>',
	    	                    '<td class="txtc">{impression}</td>',
	    	                    '<td class="txtc">{click}</td>',
	    	                    /*'<td class="txtc floor" consume_type_in="{consume_type_in}"><div class="btn-group btn-group-sm"><button type="button" class="btn btn-success state_open">开</button><button type="button" class="btn btn-default state_close">关</button></div></td>',
	    	                    '<td class="txtc floor" consume_type_in="{consume_type_in}"><div class="btn-group btn-group-sm"><button type="button" class="btn btn-success state_open">开</button><button type="button" class="btn btn-default state_close">关</button></div></td>',
	    	                    '<td class="txtc floor" consume_type_in="{consume_type_in}"><div class="btn-group btn-group-sm"><button type="button" class="btn btn-success state_open">开</button><button type="button" class="btn btn-default state_close">关</button></div></td>',*/
		                     	'<td class="txtc">',
		                     		'<a class="ibtn showchart" href="javascript:;" title="查看报表"><span class="glyphicon glyphicon-list-alt"></span></a>',
		                     		'<a class="ibtn editbtn" href="javascript:;" title="修改"><span class="glyphicon glyphicon-pencil"></span></a>',
		                     		'<a class="ibtn viewbtn" style="display:none;" href="javascript:;" title="查看"><span class="glyphicon glyphicon-eye-open"></span></a>',
		                     		'<a class="ibtn delbtn" href="javascript:;" title="删除"><span class="glyphicon glyphicon-remove"></span></a>',
	                     		'</td>',
		                     '</tr>'].join('');
    
    	var grid = Grid.init({
            holder:'strategyList',
            bodyTemplate:bodyTemplate,
            dataSource:'/trp/strategy/get_list/',
            pageTemplate:semi.grid.pageTemplate,
            sendOnPageSize: true
        });
    	
    	grid.modifyModel(function(data){
    		if(data.hours.length==24){
    			data.hours = '全天';
    		}else{
    			data.hours = semi.utils.shorterNums(data.hours).join(',');
    		}
    		var areas = [];
    		if(!semi.utils.isEmpty(data.areas)){
    			$.each(data.areas, function(i,n){
    				areas.push(n.name);
    			});
    		}
    		data.areas = areas.join(',');
    		var words = [];
    		if(!semi.utils.isEmpty(data.words)){
	    		$.each(data.words, function(i,n){
					words.push(n.name);
				});
    		}
    		data.words = words.join(',');
    		if(data.state=='未开始'){
        		data.state = '<span class="label label-default" style="font-size:14px;">未开始</span>';
        	}else if(data.state=='投放中'){
        		data.state = '<span class="label label-success" style="font-size:14px;">投放中</span>';
        	}else if(data.state=='已结束'){
        		data.state = '<span class="label label-danger" style="font-size:14px;">已结束</span>';
        	}
    	});
    	
        grid.onfilled(function(data){
        	grid.data = data;
        	//是否可删
        	$('#strategyList tbody tr').each(function(){
        		var $this = $(this);
        		if($('.strategy_state',$this).text()!='未开始'){
        			$('.delbtn',$this).addClass('disabled').removeClass('delbtn');
        		}
        		if($('.strategy_state',$this).text()=='已结束'){
        			$('.editbtn',$this).remove();
        			$('.viewbtn',$this).show();
        		}
        	});
        	/*//平滑消耗开关： 1开   0关 
        	$('#strategyList td[consume_type_in]').each(function(){
        		var $this = $(this), consume_type_in = $this.attr('consume_type_in');
        		if(consume_type_in=='1'){
        			$('.state_open',$this).removeClass('btn-default').addClass('btn-success');
        			$('.state_close',$this).removeClass('btn-danger').addClass('btn-default');
        		}else{
        			$('.state_open',$this).removeClass('btn-success').addClass('btn-default');
        			$('.state_close',$this).removeClass('btn-default').addClass('btn-danger');
        		}
        	});
        	$('#strategyList td[consume_type_out]').each(function(){
        		var $this = $(this), consume_type_out = $this.attr('consume_type_out');
        		if(consume_type_out=='1'){
        			$('.state_open',$this).removeClass('btn-default').addClass('btn-success');
        			$('.state_close',$this).removeClass('btn-danger').addClass('btn-default');
        		}else{
        			$('.state_open',$this).removeClass('btn-success').addClass('btn-default');
        			$('.state_close',$this).removeClass('btn-default').addClass('btn-danger');
        		}
        	});
        	$('#strategyList td[consume_type_ally]').each(function(){
        		var $this = $(this), consume_type_ally = $this.attr('consume_type_ally');
        		if(consume_type_ally=='1'){
        			$('.state_open',$this).removeClass('btn-default').addClass('btn-success');
        			$('.state_close',$this).removeClass('btn-danger').addClass('btn-default');
        		}else{
        			$('.state_open',$this).removeClass('btn-success').addClass('btn-default');
        			$('.state_close',$this).removeClass('btn-default').addClass('btn-danger');
        		}
        	});*/
        	$('.ibtn').tooltip();
        });
    	
	    /* 数据搜索 */
	    var filterData = function(pno){
	    	var txt = $('.searchbox-ipt').val(),
	    		d = $('[name=reservation]').val().split(' 至 '),
	    		state = $('.pageheader select[name=state]').val(),
	        	pageSize = 20;
	    	
	    	if($('select.page-size').size()>0){
	    		pageSize = $('select.page-size').val();
	    	}
	        grid.send({
	        	traffic_id: traffic_id,
	        	resource: resource_id,
	    		search_text: txt,
	    		start: d[0],
        		end: d[1],
	    		pageSize: pageSize,
	    		state: state,
	    		pageNo: pno || 1
	    	});
	    };
	    filterData();
	    
	    //按购买周期模糊搜索
        $('input[name=reservation]').daterangepicker({
            format:'YYYY-MM-DD'
        }, function(start, end, label) {
        	console.log(start.toISOString(), end.toISOString(), label);
        	filterData();
        });
	    
	    //按名称、属性模糊搜索
        $('.pageheader .searchbox-btn').on('click', function(){
        	filterData();
        });
        $('.pageheader .searchbox-ipt').on('keyup', function(e){
        	if(e.keyCode==semi.keyCode.ENTER){
        		filterData();
        	}
        });
        
        //按审核状态搜索
        $('.pageheader select[name=state]').on('change', function(){
        	filterData();
        }).select2();
	    
        /* 新建投放策略 */
        $(document).on('click', '.createstrategy', function(){
        	require.async('./common/popstrategy', function($popstrategy){
        		$popstrategy.add({
        			type: 'platform',
        			traffic_id: traffic_id,
    	    		resource_id: resource_id,
        			success: function(){
        				filterData();
        			}
        		});
        	});
        });
        
        /* 修改投放策略 */
        $(document).on('click', '.editbtn,.viewbtn', function(){
        	var $this = $(this), $tr = $this.parents('tr:first'), strategy_id = $tr.attr('strategy_id'), state = $('.strategy_state',$tr).text(), isview = false;
        	if($this.hasClass('viewbtn'))isview = true;
        	require.async('./common/popstrategy', function($popstrategy){
        		$popstrategy.edit({
        			type: 'platform',
        			strategy_id: strategy_id,
        			traffic_id: traffic_id,
    	    		resource_id: resource_id,
    	    		isview: isview,
    	    		state: state,
        			success: function(){
        	        	filterData(semi.utils.gridPage(grid));
        	        }
    			});
        	});
        });
	    
	    /* 报表-dialog */
        var $popchart = require('./common/popchart');
        $(document).on('click', '.showchart', function(){
        	var $this = $(this), $tr = $this.parents('tr:first'), strategy_id = $tr.attr('strategy_id');
        	$popchart({
            	title: '策略报表',
            	btn: $this,
//            	dataurl: '/trp/position/get_strategy_report_in_position',
            	dataurl: '/trp/strategy/report',
            	type: 'strategy',
            	idname: 'position_id',
            	idvalue: -1,
            	other:{
            		strategy_id: strategy_id
            	},
            	hastype: true
            });
        });
        
        /* 删除-dialog */
        $(document).on('click', '.delbtn', function(){
        	var $this = $(this), $tr = $this.parents('tr:first'), strategy_id = $tr.attr('strategy_id');
        	var $dlg = $dialog.confirm({
        		title: '删除策略',
                content: '您确定要删除该策略吗？',
                ok: function(){
                	var $dlg = this;
                	$ajax.ajax({
                    	url: '/trp/strategy/delete/',
                        data: {
                        	id: $dlg.strategy_id
        				},
                        success: function(){
                        	$dlg.close().remove();
                        	$dialog.alert('删除策略成功！','success');
                        	filterData(semi.utils.gridPage(grid));
                        },
	                    error: function(){
	                    	$dlg.close().remove();
	                    	$dialog.alert('删除策略失败，请稍后重试！','warning');
	                    }
                    });
                	return false;
                }
        	});
        	$.extend($dlg,{strategy_id:strategy_id});
        });
        
    });
});